/**
 * This file is a part of qloudgen-rul. 
 * You can redistribute qloudgen-rul and/or modify it under the terms of the Lesser GNU General Public License version 3. 
 * qloudgen-rul is distributed WITHOUT ANY WARRANTY. 
 *
 * See the Lesser GNU General Public License for more details at http://www.gnu.org/licenses/. 
 **/
package org.qloudgen.rul.drools.rule;

import org.kie.api.builder.KieScanner;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * <p>Title: Drools Implementation</p>
 * <p>Description: Wrappered jboss drools rules engine for maven project and using default scanner implementation.</p>
 * <p>Copyright: qloudgen/rul 2014</p>
 * @author <a href="mailto:hyq.dd.sun@gmail.com">glue.3M</a>
 */
public final class DroolsMavenRuleRunner extends AbstractDroolsMavenRuleRunner {
	/**
	 * Log output.
	 */
	private final static Logger logger = LoggerFactory.getLogger( DroolsMavenRuleRunner.class );
	/**
	 * Default polling interval is 5 minutes.
	 */
	private final static long DEFAULT_POLLING_INTERVAL = 300000L;
	/**
	 *
	 */
	private long pollingInterval = 0L;
	
	/**
	 * Default constructor.
	 */
	public DroolsMavenRuleRunner() {
		this( DEFAULT_POLLING_INTERVAL );
	}
	
	/**
	 * Custom constructor.
	 *
	 * @param pollingInterval
	 */
	public DroolsMavenRuleRunner(long pollingInterval) {
		this.pollingInterval = pollingInterval;
	}
	
	/**
	 * Sets current polling interval.
	 *
	 * @param pollingInterval
	 */
	public void setPollingInterval(final long pollingInterval) {
		this.pollingInterval = pollingInterval;
	}
	
	/**
	 * Returns current polling interval.
	 *
	 * @return long
	 */
	public long getPollingInterval() {
		return pollingInterval;
	}
	
	/**
	 * Initialize current scanner.
	 *
	 * @param scanner
	 */
	protected void initInternal(KieScanner scanner) {
		logger.info( "Starting initialize drools default repository scanner...{}" , scanner );
		/**
		 * When using LATEST or RELEASE don't need force to scan now.
		 */
		//scanner.scanNow();
		/**
		 * Drools using polling interval as delay time.
		 */
		scanner.start( pollingInterval );
	}
	
	/**
	 * Destroy current scanner.
	 *
	 * @param scanner
	 */
	protected void destroyInternal(KieScanner scanner) {
		logger.info( "Starting destroy drools default repository scanner...{}" , scanner );
		/**
		 * Force do stop now.
		 */
		scanner.stop();
	}
	
	/**
	 * Returns the String representation.
	 *
	 * @return String
	 */
	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append( "[" );
		builder.append( super.toString() );
		builder.append( "-->" );
		builder.append( "pollingInterval=" ).append( pollingInterval );
		builder.append( "]" );
		//
		return builder.toString();
	}
}